/************************* (C) COPYRIGHT 2008 Domain **************************
 * File Name   : debug.h                                                      *
 * Author      : Domain                                                       *
 * E-mail      : do.main@msn.com                                              *
 * Version     : 1.0                                                          *
 * Date        : 2008/5/27                                                    *
 * Description :                                                              *
 ******************************************************************************/

#ifndef __DEBUG_H
#define __DEBUG_H

#include "stm32f10x_type.h"

#define DEBUG_LEVEL_OFF                   0x00
#define DEBUG_LEVEL_ERR                   0x01
#define DEBUG_LEVEL_MSG                   0x02
#define DEBUG_LEVEL_INFO                  0x03
#define DEBUG_LEVEL_DETAIL                0x04

#define DEBUG_MOBILE_PARAM                0x1

#ifdef DEBUG
 
  #include <stdio.h>
  #include "common.h"
  #include "parameter.h"
  #include "stm32f10x_lib.h"

  //#define DEBUG_LEVEL_OFF         (u8)0xFF
  //#define DEBUG_LEVEL_ALL         (u8)0x01
  //#define DEBUG_LEVEL_KEY         (u8)0xF0

  //extern u8 DebugLevel;
  #ifndef BOOTLOADER
  #define CheckDebugLevel(level)       if (GlobalParameter.DebugLevel >= (level))
  #else
  #define CheckDebugLevel(level)
  #endif /* BOOTLOADER */

  #ifdef DEBUG_DETAIL
  #define DebugStart()                                                           \
          do {                                                                   \
            fprintf(F_DEBUG, "\r\n");                                            \
            fprintf(F_DEBUG, ">>>>> FILE: %s, LINE: %d, FUNCTION: %s <<<<<\r\n", \
			        DebugFilename(__FILE__), __LINE__, __FUNCTION__);                  \
		  } while(0)
  #else
  #define DebugStart()
  #endif
  
  #define DebugEnd()

  #define DebugPrintf(level, ...)                                                \
          do {                                                                   \
            CheckDebugLevel(level)                                               \
            {                                                                    \
              /*if (GlobalParameter.DebugLevel >= DEBUG_LEVEL_DETAIL)*/          \
              DebugStart();                                                      \
              fprintf(F_DEBUG, __VA_ARGS__);                                     \
              DebugEnd();                                                        \
            }                                                                    \
		  } while(0)
  
  #define DebugData(level, array, length, ...)                                   \
          do {                                                                   \
            CheckDebugLevel(level)                                               \
            {                                                                    \
              fprintf(F_DEBUG, __VA_ARGS__);                                     \
              debugData((array), (length));                                      \
            }                                                                    \
          } while(0)
  
  #define DebugAssert(x, y)                                                      \
          do {                                                                   \
            /*CheckDebugLevel(DEBUG_LEVEL_ERR)*/                                 \
            {                                                                    \
              if (!(y))                                                          \
                AssertFailed(x, __FILE__, __LINE__);                             \
            }                                                                    \
          } while(0)

  void DebugInit(void);
  void debugData(const u8* array, const u16 length);
  u8* DebugFilename(u8* file);

#else /* DEBUG */  

  #define DebugStart()
  #define DebugPrintf(...)
  #define DebugInit()
  #define DebugData(level, array, length, ...)
  #define DebugAssert(x, y)

#endif /* DEBUG */

#endif /* __DEBUG_H */

/************************* (C) COPYRIGHT 2008 Domain ******* END OF FILE ******/
